package com.xmwzw.catpu;

import android.Manifest;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.view.KeyEvent;
import android.widget.RadioGroup;
import android.widget.Toast;

import com.tbruyelle.rxpermissions2.RxPermissions;
import com.xmwzw.catpu.databinding.ActivityMainBinding;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends BaseActivity {
    private String downloadUrl = "http://oss.pgyer.com/09c7411a25c4dae4a9f9bbaaa0378ca7.apk?auth_key=1586176056-84c334cd22cf2d7f278b015de390d76c-0-19557f97af1f47cb08c96d269fa12c79&response-content-disposition=attachment%3B+filename%3Dapp-debug.apk";
    private List<Fragment> fragments = new ArrayList<>(2);
    private ActivityMainBinding mainBinding;
    private long firstTime;

    @Override
    public int getLayoutId() {
        return R.layout.activity_main;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        RxPermissions rxPermissions = new RxPermissions(this);
        rxPermissions.request(Manifest.permission.READ_EXTERNAL_STORAGE,
                Manifest.permission.WRITE_EXTERNAL_STORAGE,Manifest.permission.INTERNET,
                Manifest.permission.READ_PHONE_STATE
        );

        mainBinding = getBaseViewBinding();
        fillData();
        initViews();


    }


    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        long secondTime = System.currentTimeMillis();

        if (keyCode == KeyEvent.KEYCODE_BACK) {
            if (secondTime - firstTime < 2000) {
                System.exit(0);
            } else {
                Toast.makeText(getApplicationContext(), "再按一次返回键退出", Toast.LENGTH_SHORT).show();
                firstTime = System.currentTimeMillis();
            }

            return true;
        }

        return super.onKeyDown(keyCode, event);
    }

    private void fillData() {
        fragments.add(new HomeFragment());
        fragments.add(new MyCenterFragment());
        mainBinding.viewpager.setAdapter(new MyPageAdapter(getSupportFragmentManager(), fragments));
        mainBinding.viewpager.setCurrentItem(0);

    }

    private void initViews() {
        mainBinding.viewpager.setNoScroll(true);

        mainBinding.rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int i) {
                switch (i) {
                    case R.id.rb1:
                        mainBinding.viewpager.setCurrentItem(0);
                        break;
                    case R.id.rb2:
                        mainBinding.viewpager.setCurrentItem(1);
                        break;
                }
            }
        });

    }

    public static class MyPageAdapter extends FragmentPagerAdapter {
        private List<Fragment> fragments;

        public MyPageAdapter(FragmentManager fm, List<Fragment> fragments) {
            super(fm);
            this.fragments = fragments;
        }

        @Override
        public Fragment getItem(int i) {
            return fragments.get(i);
        }

        @Override
        public int getCount() {
            return fragments.size();
        }
    }
}
